{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.stats.mstats import winsorize\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.linear_model import LinearRegression\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import scipy.stats as st\n",
    "from alphalens import performance\n",
    "from alphalens import plotting\n",
    "from alphalens import tears\n",
    "from alphalens import utils"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python3.6/site-packages/ipykernel_launcher.py:3: UserWarning: 'get_fundamentals' is deprecated, and will be removed soon. use get_factor instead.\n",
      "  This is separate from the ipykernel package so we can avoid doing imports until\n",
      "/usr/local/lib/python3.6/site-packages/rqdatac/services/financial.py:480: UserWarning: Panel is  removed after pandas version 0.25.0.the  default value of 'expect_df' will change to True in the future.\n",
      "  warnings.warn(\"Panel is  removed after pandas version 0.25.0.\"\n"
     ]
    }
   ],
   "source": [
    "# 获取所有的股票\n",
    "q = query(fundamentals.eod_derivative_indicator.pe_ratio)\n",
    "fund=get_fundamentals(q,entry_date='2017-01-03')[:,'2017-01-03',:]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pe_ratio</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>000010.XSHE</th>\n",
       "      <td>160.159</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000014.XSHE</th>\n",
       "      <td>148.475</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000006.XSHE</th>\n",
       "      <td>16.767</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000008.XSHE</th>\n",
       "      <td>50.2749</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000012.XSHE</th>\n",
       "      <td>29.658</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            pe_ratio\n",
       "000010.XSHE  160.159\n",
       "000014.XSHE  148.475\n",
       "000006.XSHE   16.767\n",
       "000008.XSHE  50.2749\n",
       "000012.XSHE   29.658"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1、获取这一天因子数据\n",
    "# 2、获取2017-01-03与2017-01-04的价格数据，计算2017-01-04的收益率（下期收益率）\n",
    "# 3、计算相关系数\n",
    "fund.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 首先记录下下期的收益值\n",
    "price_now=get_price(list(fund.index),start_date='2017-01-03',end_date='2017-01-03',fields='close')\n",
    "price_next=get_price(list(fund.index),start_date='2017-01-04',end_date='2017-01-04',fields='close')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "price_next=price_next.T\n",
    "price_now=price_now.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>date</th>\n",
       "      <th>2017-01-03 00:00:00</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>002406.XSHE</th>\n",
       "      <td>7.6910</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000957.XSHE</th>\n",
       "      <td>15.4092</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>600648.XSHG</th>\n",
       "      <td>19.0355</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>603696.XSHG</th>\n",
       "      <td>18.3017</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>002537.XSHE</th>\n",
       "      <td>14.2783</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "date         2017-01-03\n",
       "002406.XSHE      7.6910\n",
       "000957.XSHE     15.4092\n",
       "600648.XSHG     19.0355\n",
       "603696.XSHG     18.3017\n",
       "002537.XSHE     14.2783"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "price_now.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算收益率, 填充缺失值\n",
    "price_now.iloc[:,0].fillna(price_now.iloc[:,0].mean(),inplace=True)\n",
    "price_next.iloc[:,0].fillna(price_next.iloc[:,0].mean(),inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算收益率return\n",
    "return_price=price_next.iloc[:,0]/price_now.iloc[:,0]-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "002406.XSHE    0.003459\n",
       "000957.XSHE    0.018878\n",
       "600648.XSHG    0.003509\n",
       "603696.XSHG    0.011387\n",
       "002537.XSHE    0.004398\n",
       "dtype: float64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "return_price.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SpearmanrResult(correlation=-0.004620600368218553, pvalue=0.7990189388957557)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算相关系数,是一个值，在[-1, 1]  IC \n",
    "st.spearmanr(fund['pe_ratio'],return_price)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
